package com.miku.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.miku.blog.domain.Social;
import com.miku.blog.domain.pojo.SystemConstance;
import com.miku.blog.service.SocialService;
import com.miku.blog.mapper.SocialMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;

/**
* @author Miku
* @description 针对表【tb_social】的数据库操作Service实现
* @createDate 2022-06-08 08:47:10
*/
@Service
public class SocialServiceImpl extends ServiceImpl<SocialMapper, Social>
    implements SocialService{


    @Override
    public List<Social> getSocialList(Integer webSiteId){
        LambdaQueryWrapper<Social> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Social::getWebsiteId,webSiteId)
                .eq(Social::getIsHidden, SystemConstance.NOT_HIDDEN);
        return list(queryWrapper);
    }

    @Override
    public boolean updateSocial(Social social) {
        Integer isHidden = social.getIsHidden();
        if (Objects.isNull(isHidden)
                || (!SystemConstance.IS_HIDDEN.equals(isHidden) && !SystemConstance.NOT_HIDDEN.equals(isHidden))
        ){
            return false;
        }
        String socialUrl = social.getSocialUrl();
        LambdaUpdateWrapper<Social> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Social::getWebsiteId,SystemConstance.WEBSITE_ID)
                .eq(Social::getSocialType,social.getSocialType())
                .set(Social::getIsHidden,isHidden)
                .set(StringUtils.hasText(socialUrl),Social::getSocialUrl,socialUrl);
        return update(updateWrapper);
    }
}




